
class RecordAndRestore {
    /**
     * 保存每个router的操作记录
     * @param routerId
     * @param data
     * @returns {boolean}
     */
    saveRecord(routerId,data){
           if(this.isRepeatRouter( routerId , data) ) {
               return false;
           }

           let doRecord = window.CONFIG.doRecord;
           if( typeof doRecord[routerId] !="undefined") {
               doRecord[routerId].push( data );
           }else{
               doRecord[routerId] = [];
               doRecord[routerId].push( data );
           }
          window.CONFIG.doRecord = doRecord;
    }

    /**
     *恢复上一次的操作记录
     * @returns {boolean}
     */
    restore() {
        let routerId = CONFIG.currentOpearateRouterId;
        CONFIG.doRecord[routerId].pop();
        let routers = CONFIG.doRecord[routerId].pop();
        if( routers == undefined ) {
            return false;
        }
        let routerIdBgColor = OBJECT.routersBgColorMap[routerId];
        this.romveOldRoutersAndAreaBg(routerId);
        let  different = this.getDifferentPointFromLastOpreate( routerId , routers );
        //生成标记or剔除标记
        for( let i=0; i<different.data.length; i++ ) {
                if(different.type=='OUT') {
                    MarkerObj.create( different.data[i].lat , different.data[i].lng );
                    Session.getInstance().saveRouteRemovePoints(different.data[i]);
                }else{
                    MarkerObj.remove( different.data[i].lat , different.data[i].lng );
                     Session.getInstance().deleteRouteRemovePoints(different.data[i]);
                }
        }
        Polygon.draw(routers , map , routerId ,routerIdBgColor );
        Router.getInstance().calculateAndDisplayRoute(routers , routerId ,routerIdBgColor);
    }




    /**
     * 取出本次操作和上次操作差异值
     * @param routerId
     * @param routers
     * @returns {Array}
     */
    getDifferentPointFromLastOpreate ( routerId , routers) {
             let ret={};
             ret.data = [];
             let layerArr=[];
             let outArr=[];
             let current = OBJECT.directionsDisplay[routerId].routers;
             if( current.length > routers.length ) {
                 ret.type="OUT";  //和前一次操作对比要剔除一个
                  layerArr = current;
                  outArr = routers;
             }else{
                  layerArr = routers;
                  outArr = current;
                 ret.type="IN";   //和前一次操作对比要增加一个
             }
            // console.log(current,routers)
             for(var i = 0; i <layerArr.length; i++){
                let obj = layerArr[i];
                let lat = obj.lat;
                let lng = obj.lng;
                let isExist = false;
                for(let j = 0; j < outArr.length; j++){
                    let aj = outArr[j];
                    if(aj.lat == lat && aj.lng==lng){
                        isExist = true;
                        break;
                    }
                }
                if(!isExist){
                    ret.data.push(obj);
                }
            }
            return ret;

    }
    /**
     * 清除上一次操作的对象
     * @param routerId
     */
    romveOldRoutersAndAreaBg( routerId){
        OBJECT.bermudaTriangle[routerId].setMap( null );
        OBJECT.directionsDisplay[routerId].setMap( null );
    }

    /**
     * 检测本次操作是否和上次操作一样
     * @param routerId
     * @param data
     * @returns {boolean}
     */
    isRepeatRouter( routerId, data) {
        let doRecord = window.CONFIG.doRecord;

        if( typeof doRecord[routerId] == 'undefined' || doRecord[routerId] == false ) {
            return false;
        }
        var len = doRecord[routerId].length;
        let lastDo = doRecord[routerId][len-1];
        if( lastDo.length != data.length ) {
            return false;
        }
        for ( let i=0; i<data.length;i++ ) {
            if( lastDo[i].lat== data[i].lat && lastDo[i].lng== data[i].lng ) {
                return true;
            }
        }
        return false;

    }
}
